/**
 *区域树形菜单查询
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");

module.exports = async function (params) {
    let sql = `SELECT
                    rf.range_id AS rangeId,
                    rf.range_name AS rangeName,
                    rf.lng,
                    rf.lat,
                    rf.height,
                    rf.parent_id AS parentId,
                    rf.top_level_id AS topLevelId,
                    CONCAT("g_",uf.group_id) AS groupId,
                    uf.user_id AS userId,
                    uf.user_name AS userName,
                    uf.mobile
                FROM
                    range_file rf
                LEFT JOIN user_file uf ON uf.user_id = rf.user_id AND uf.delete_sign = 1
                WHERE
                rf.delete_sign = 1
                AND rf.domain_id = ${params.userDomainId};SELECT domain_name AS domainName FROM domain_file WHERE domain_id = ${params.userDomainId}`
                /*SELECT
                    count(*) AS count
                FROM user_file uf
                INNER JOIN user_group ug ON ug.group_id = uf.group_id AND ug.is_manager = 1 AND ug.delete_sign = 1
                WHERE uf.delete_sign = 1 AND user_id = ${params.userId}*/;
    let result = await conn(sql);
    /*let rangeList = result[0];
     let isManager = result[1][0].count > 0; */
    let dataList = childrenListSort(0,result[0]);
    let domainName = result[1][0].domainName;
    /*if(isManager){
        dataList = childrenListSort(0,rangeList);
    }else{
        let userRangeList = _.filter(rangeList, {userId: params.userId});
        let allUserRangeList = [];
        for(const userRange of userRangeList){
            allUserRangeList = allUserRangeList.concat(getChildrenConnectList(userRange.rangeId,rangeList,[],0));
            allUserRangeList.push(userRange);
            allUserRangeList = allUserRangeList.concat(getParentConnectList(userRange,rangeList));
        }
        allUserRangeList = _.uniq(allUserRangeList);
        dataList = childrenListSort(0,allUserRangeList);
    }*/
    return { success: true ,dataList:dataList,domainName:domainName};
};
function childrenListSort(childId,rangeList){
    let childrenList = _.filter(rangeList,{parentId:childId})
    for(const childIndex in childrenList){
        childrenList[childIndex].children = childrenListSort( childrenList[childIndex].rangeId,rangeList);
    }
    return childrenList;
}
/*
function getChildrenConnectList(rangeId,rangeList,childrenConnectList,level){
    let childrenList = _.filter(rangeList,{parentId:rangeId})
    for(const childIndex in childrenList){
        childrenConnectList = childrenConnectList.concat(getChildrenConnectList( childrenList[childIndex].rangeId,rangeList,childrenConnectList,level));
        childrenConnectList.push(childrenList[childIndex]);
    }
    return childrenConnectList;
}
function getParentConnectList(self,rangeList){
    let parentConnectList = [];
    let parentObj = {
        parentId:self.parentId
    };
    while (parentObj && parentObj.parentId !== 0 ){
        let parent =  _.find(rangeList,function (data){
            return data.rangeId === parentObj.parentId;
        })
        if(parent) parentConnectList.push(parent)
        parentObj = parent;
    }
    return parentConnectList;
}*/
